<template>
    <div>
        <div style="margin-top: 20px; width: 100%">
            <el-form :inline="true" :model="ruleForm" style="font-size: 15px; width: 100%" class="query-form">
                <div>
                    <el-form-item label="到票编号">
                        <el-input v-model="page.arrivalNumber" />
                    </el-form-item>
                </div>
                <el-form-item>
                    <el-button type="primary" @click="fetchData">查询</el-button>
                    <el-button type="primary" @click="empty">清空</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div style="width: 80px">
            <el-button type="primary" @click="AddInfo()">新增</el-button>
        </div>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="aid" label="序号" width="60" />
            <el-table-column prop="arrivalNumber" label="到票编号" />
            <el-table-column prop="arrivalName" label="到票主题" />
            <el-table-column prop="arrivaltype" label="发票类型">
                <template #default="scope">
                    <span v-if="scope.row.arrivaltype === 1">增值税专用发票</span>
                    <span v-else-if="scope.row.arrivaltype === 2">增值税普通发票</span>
                    <span v-else-if="scope.row.arrivaltype === 3">增值税电子发票</span>
                    <span v-else-if="scope.row.arrivaltype === 4">增值税普通发票(免税)</span>
                    <span v-else-if="scope.row.arrivaltype === 5">其他</span>
                </template>
            </el-table-column>
            <el-table-column prop="arrivalTime" label="到票日期">
                <template #default="scope">
                    {{ scope.row.arrivalTime.substring(0, 10) }}
                </template>
            </el-table-column>
            <el-table-column prop="drawer" label="开款方">
                <template #default="scope">
                    <span v-if="scope.row.drawer == 1">华为</span>
                    <span v-else-if="scope.row.drawer == 2">小米</span>
                    <span v-else-if="scope.row.drawer == 3">宜家</span>
                    <span v-else-if="scope.row.drawer == 4">阿里</span>
                    <span v-else-if="scope.row.drawer == 5">其他</span>
                </template>
            </el-table-column>
            <el-table-column prop="invoiceamount" label="开票金额" />
            <el-table-column prop="invoicenumber" label="发票号" />
            <el-table-column prop="remark" label="备注" />
            <el-table-column label="操作" width="150">
                <template #default="scope">
                    <el-button size="small" @click="editItem(scope.row)">编辑</el-button>
                    <el-button size="small" @click="DelFrom(scope.row.aid)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div>
            <el-pagination v-model:current-page="page.PageIndex" v-model:page-size="page.PageSize"
                :page-sizes="[10, 20, 30, 40]" layout="total, sizes, prev, pager, next, jumper" :total="page.totalCount"
                @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>
</template>
<script setup lang="ts">
import router from '@/router';
import axios from 'axios';
import { ElMessage, ElMessageBox, ElTable, ElTableColumn, ElPagination } from 'element-plus';
import { onMounted, ref, reactive, computed } from 'vue';
const apiUrl = import.meta.env.VITE_APP_API_URL;
const tableData = ref([]);

// 查询表单数据
const page = reactive({
    PageIndex: 1,
    PageSize: 10,
    totalCount: 0,
    totalPage: 0,
    arrivalNumber: '',
});

// 整合显示、查询和分页的方法
const fetchData = () => {
    axios.get(apiUrl +'/T5/ProductInfo/GetArrivalmanagement', { params: page })
        .then((res) => {
            tableData.value = res.data.data;
            page.totalCount = res.data.totalCount;
            page.totalPage = res.data.totalPage;
        })
};

const empty = () => {
    page.arrivalNumber = '',
        fetchData();
};

const handleSizeChange = (val: number) => {
    console.log(`${val} items per page`);
    page.PageSize = val;
    fetchData();
};
const handleCurrentChange = (val: number) => {
    console.log(`current page: ${val}`);
    page.PageIndex = val;
    fetchData();
};

//逻辑删除供应商
const DelFrom = (aid: any) => {
    axios.put(apiUrl +'/T6/ProductInfos/DelArrivalmanagement', { aid: aid })
        .then((res) => {
            if (res.data.code == 204) {
                ElMessage({
                    message: '删除成功',
                    type: 'success',
                })
                fetchData();
            } else {
                ElMessage({
                    message: '删除失败',
                    type: 'error',
                })
            }
        })
}

const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
    aid: 0,
    arrivalNumber: '',
    arrivalName: '',
    arrivaltype: null,
    arrivalTime: new Date(),
    drawer: '',
    invoiceamount: 0,
    invoicenumber: '',
    remark: '',
    sellId: 0,
    isDelete: false,
})

const AddInfo = () => {
    router.push('/Addarrival');
}

onMounted(() => {
    fetchData();
})
</script>
<style lang="">

</style>